import {brandAction2} from '../funnames'
import Compator from '../../utils/compare'
import Character from '../../utils/character'
import globalfun from '../../utils/globalfun'

const state = {
  hotBrandList: [],
  brandList: [],
  letterMap: {},
  lettersList: [],
}

const mutations = {
  [brandAction2.ACTION_CONDITION_BRAND_UPDATE](state, brandData) {
    state.brandList = brandData.children
    for (var i = 0; i < state.brandList.length; i++) {
      if (Character.chkstrChinaese(state.brandList[i].name)) {
        state.brandList[i].letter = Character.makePy(state.brandList[i].name)[0].charAt(0)
      } else {
        state.brandList[i].letter = state.brandList[i].name.charAt(0).toUpperCase()
      }
      state.brandList[i].checked = false
      if (state.brandList[i].isHot == 1) {
        state.hotBrandList = state.hotBrandList.concat(state.brandList[i])
      }
    }
    Array.sort(state.brandList, Compator.compare('letter'))
    for (var j = 0; j < state.brandList.length; j++) {
      var itemLetter = state.brandList[j].letter
      if (!state.lettersList) {
        state.lettersList = new Array()
      }
      if (state.lettersList.indexOf(itemLetter) == -1) {
        state.lettersList = state.lettersList.concat(itemLetter)
      }
      if (!state.letterMap[itemLetter]) {
        state.letterMap[itemLetter] = []
      }
      state.letterMap[itemLetter] = state.letterMap[itemLetter].concat(state.brandList[j])
    }
  },
  [brandAction2.ACTION_RESET](state) {
    if (!globalfun.isNull(state.letterMap)) {
      for (var key in state.letterMap) {
        let itemList = state.letterMap[key]
        if (itemList) {
          itemList.forEach((brandData) => {
            brandData.checked = false
          })
        }
      }
    }
  }
}


const actions = {
  async getCarts({commit}) {
  }
}

export default {
  state,
  mutations,
  actions
}
